<?php namespace app\home\controller;
class Api {
	public function __construct() {
		Wx::bind();
	}

	public function handler() {
		$message = Wx::getMessage();
		//文本消息响应
		if (Wx::instance('message')->isText()) {
			$this->handlerMessage();
			$this->text($message->Content);
		}
		//菜单点击事件响应
		if (Wx::instance('message')->isClick()) {
			$this->text($message->EventKey);
		}

		//系统自动回复
		Wx::instance('message')->text(v('config.default_message'));

	}

	//回复文本消息
	public function text($content)
	{
		// Wx::instance('message')->text($Content);die;
		$keyword = Db::table('keywords')->where('keyword',$content)->first();
		if ($keyword) {
			$class='module\\'.$keyword['module'].'\Process';
			(new $class()) -> handler($keyword['kid']);
			die;
		}
	}

	//处理所有接受文本信息
	public function handlerMessage()
	{
		$res = Db::table('plugin')->where('is_handler',1)->get();
		if ($res) {
			foreach ($res as $r ) {
				$class = 'module\\'.$r['name'].'\Handler';
				if (class_exists($class)) {
					$obj = new $class;
					$obj->run();
				}
			}
		}
	}
}